package bots.neural;

import bots.AbstractBot;
import model.*;
import org.apache.log4j.Logger;
import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.basic.BasicMLData;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.ml.train.MLTrain;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
import util.Util;

import java.util.List;

/**
* @author Alex Vikharev alex.vikharev@gmail.com
*         created 02.02.12
*/
public class FirstNNBot extends AbstractBot implements TeachablePlayer {
    private static final Logger log = Logger.getLogger(FirstNNBot.class);
    private final BasicNetwork network;


    private long randomMoves;
    private long totalMoves;

    public void resetStatistics() {
        randomMoves = 0;
        totalMoves = 0;
    }

    public BasicNetwork getNetwork() {
        return network;
    }

    public FirstNNBot(String name, GameType gameType) {
        super(name, gameType);
        network = new BasicNetwork();
        network.addLayer(new BasicLayer(17 * 5));
        network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 34 * 5));
        network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 17 * 5));
        network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 1));
        network.getStructure().finalizeStructure();
        network.reset();
    }

    @Override
    public void teach(Iterable<Position> goodMoves, Iterable<Position> badMoves) {
        MLDataSet dataSet = new BasicMLDataSet();

        MLData idealGood = new BasicMLData(new double[]{1.0});
        for (Position goodMove : goodMoves) {
            dataSet.add(new BasicMLData(Util.BITWISE_CONVERTER.apply(goodMove)), idealGood);
        }

        MLData idealBad = new BasicMLData(new double[]{0.0});
        for (Position badMove : badMoves) {
            dataSet.add(new BasicMLData(Util.BITWISE_CONVERTER.apply(badMove)), idealBad);
        }

        int epoch = 0;
        MLTrain train = new ResilientPropagation(network, dataSet);
        do {
            train.iteration();
            log.info("Training network, epoch: " + epoch + " error: " + train.getError());
            epoch++;
        } while (train.getError() > 0.15);
        log.info("Training is over, error: " + train.getError());
    }

    @Override
    public Position setFigure(Field field, Figure figure) {
        List<Figure> freeFigures = freeFigures(field, figure);
        Position bestMove = null;
        double bestResult = 0;
        for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 4; y++) {
                if (field.get(x, y) == null) {
                    if (freeFigures.isEmpty()) {
                        bestMove = new Position(field.addFigure(x, y, figure), null);
                    } else {
                        for (Figure freeFigure : freeFigures) {
                            Position move = new Position(field.addFigure(x, y, figure), freeFigure);
                            double v = computeNetwork(move);
                            if (v > bestResult) {
                                bestMove = move;
                                bestResult = v;
                            }
                        }
                    }
                }
            }
        }
        totalMoves++;
        if (bestMove != null) {
            return bestMove;
        } else {
            randomMoves++;
            return setRandomFigure(field, figure);
        }
    }

    public double getRandomMovesPercent() {
        return randomMoves * 1.0 / totalMoves;
    }

    private double computeNetwork(Position move) {
        MLData data = new BasicMLData(Util.BITWISE_CONVERTER.apply(Util.getCanonical(move)));
        MLData result = network.compute(data);
        return result.getData(0);
    }


}
